﻿/* 
 * Since 2008 - 2012
 */

package com.hh.bss.project.service.impl;

import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.servlet.http.HttpServletRequest;

import com.hh.bss.auth.client.OnlineUser;
import com.hh.bss.auth.entity.SystemUser;
import com.hh.bss.auth.service.SystemUserService;
import com.hh.bss.base.*;
import com.hh.bss.common.ConstantDic;
import com.hh.bss.common.SystemConstant;
import com.hh.bss.common.cache.SystemDicCache;
import com.hh.bss.common.page.*;
import com.hh.bss.common.page.impl.*;
import com.hh.bss.config.Environment;
import com.hh.bss.customer.dao.CompanyDeptTitleDao;
import com.hh.bss.customer.entity.CompanyDept;
import com.hh.bss.customer.entity.CompanyDeptTitle;
import com.hh.bss.customer.entity.CustomerContract;
import com.hh.bss.project.dao.*;
import com.hh.bss.project.entity.*;
import com.hh.bss.project.service.*;
import com.hh.bss.project.vo.query.*;
import com.hh.bss.util.*;



import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;


@Service
@Transactional
public class ContractJobServiceImpl extends BaseBusinessService<ContractJob,java.lang.Integer>  implements ContractJobService<ContractJob,java.lang.Integer>{

	private ContractJobDao contractJobDao;
	private ContractJobPartnerDao contractJobPartnerDao;
	private ContractJobPartnerService contractJobPartnerService;
	private SearchProjectService searchProjectService;
	private ContractJobOfferPartnerService jobOfferPartnerService;
	/**增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,请注意大小写*/
	private CompanyDeptTitleDao companyDeptTitleDao;
	
	private SystemUserService systemUserService;
	
	public void setSystemUserServiceImpl(SystemUserService service) {
		this.systemUserService = service;
	}

	public void setCompanyDeptTitleDaoImpl(CompanyDeptTitleDao dao) {
		this.companyDeptTitleDao = dao;
	}
	public void setContractJobPartnerDaoImpl(ContractJobPartnerDao dao) {
		this.contractJobPartnerDao = dao;
	}
	public void setContractJobDaoImpl(ContractJobDao dao) {
		this.contractJobDao = dao;
	}
	public void setContractJobPartnerServiceImpl(ContractJobPartnerService sevice) {
		this.contractJobPartnerService = sevice;
	}
	public void setSearchProjectServiceImpl(SearchProjectService sevice) {
		this.searchProjectService = sevice;
	}
	public void setContractJobOfferPartnerServiceImpl(ContractJobOfferPartnerService sevice) {
		this.jobOfferPartnerService = sevice;
	}
	public EntityDao getEntityDao() {
		return this.contractJobDao;
	}
	
	@Transactional(readOnly=true)
	public Page findPage(ContractJobQuery query) {
		return contractJobDao.findGWPage(query);
	}
	public Page findJobPage(ContractJobQuery query) {
		Page page=contractJobDao.findJobPage(query);
		List<ContractJob> ContractJoblist = (List) page.getResult();
		//设置cc项目名
		Map m=null;
		for(int i=0;ContractJoblist!=null&&i<ContractJoblist.size();i++){
			m=(Map)ContractJoblist.get(i);			
			m.put("projectName", getJobProjectName((Integer)m.get("id")));
			if ((Integer)m.get("custId")!=null) m.put("companyName", getJobCustName((Integer)m.get("custId")));
		}
		return page;
	}
	private String getJobProjectName(Integer jobId){
		String sqls = "select group_concat(a.name  separator ',') pn from search_project a,contract_job_search_project b where b.job_id=? and a.id=b.project_id group by b.job_id";
		List values = new ArrayList();
		values.add(jobId);
		String name=null;
		List l=contractJobDao.getJdbcDao().getJdbcTemplate().queryForList(sqls, values.toArray());
		if (l!=null&&l.size()>0) {
			Map m=(Map)l.get(0);
			name=(String)m.get("pn");
		}
		return name;
	}
	/**
	 * 查询职位所在SL
	 * @param jobId
	 * @return
	 */
	public List getJobProjectList(Integer jobId){
		String sqls = "select a.name a,account  from search_project a,contract_job_search_project b where b.job_id=? and a.id=b.project_id group by b.job_id";
		List values = new ArrayList();
		List l=contractJobDao.getJdbcDao().getJdbcTemplate().queryForList(sqls, values.toArray());
		return l;
	}
	public Integer getJobProjectId(Integer jobId){
		String sqls = "select max(b.project_id)  from search_project a,contract_job_search_project b where b.job_id=? and a.id=b.project_id ";
		List values = new ArrayList();
		values.add(jobId);
		int pid=contractJobDao.getJdbcDao().getJdbcTemplate().queryForInt(sqls, values.toArray());
		//List l=contractJobDao.getJdbcDao().getJdbcTemplate().queryForList(sqls, values.toArray());
		return new Integer(pid);
	}
	private String getJobCustName(Integer custId){
		String sqls = "select b1.id companyId,b2.id,b1.abbr_name companyName,b1.full_name companyFullName,b1.address from  company b1,customer b2 where b2.id=? and b2.company_id=b1.id";
		List values = new ArrayList();
		values.add(custId);
		String name=null;
		List l=contractJobDao.getJdbcDao().getJdbcTemplate().queryForList(sqls, values.toArray());
		if (l!=null&&l.size()>0) {
			Map m=(Map)l.get(0);
			name=(String)m.get("companyName");
		}
		return name;
	}
	public Page findProjectJobPage(ContractJobQuery query){
		return contractJobDao.findProjectJobPage(query);
	}
	
	public Page findJobInProjectPage(ContractJobQuery query){
		return contractJobDao.findJobInProjectPage(query);
	}
	public List findProjectJob(ContractJobQuery query){
		return contractJobDao.findProjectJob(query);
	}
	public void save(ContractJob entity) throws DataAccessException{
		//System.out.println(entity.getProjectId()+"---");
		//searchProjectService.jobJoinProject(contractJobSearchProject);
		getEntityDao().save(entity);
		getEntityDao().flush();
		//和默认的sl进行关联
		List jsl=searchProjectService.getEntityDao().getHibernateTemplate().find("from ContractJobSearchProject t where t.jobId=?", entity.getId());
		if (jsl==null||jsl.size()==0){
			ContractJobSearchProject contractJobSearchProject=new ContractJobSearchProject();
			contractJobSearchProject.setProjectId(new Integer(1));
			contractJobSearchProject.setJobId(entity.getId());
			searchProjectService.getEntityDao().getHibernateTemplate().save(contractJobSearchProject);
			searchProjectService.getEntityDao().getHibernateTemplate().flush();
		}
		//----
		
		//如果有projectId,则将职位加入项目
		System.out.println(entity.getProjectId()+"-3--"+entity.getId());
		if (entity.getProjectId()!=null&&entity.getProjectId().intValue()!=0){			
			ContractJobSearchProject contractJobSearchProject=new ContractJobSearchProject();
			contractJobSearchProject.setProjectId(entity.getProjectId());
			contractJobSearchProject.setJobId(entity.getId());
			searchProjectService.jobJoinProject(contractJobSearchProject);
			contractJobPartnerDao.flush();
		}
		
		//保存MC(职位负责人)，PC(合作顾问)，BA(业务助理)
		if (ObjectUtils.isNotEmpty(entity.getJobManager())){
			ContractJobPartner contractJobPartner=new ContractJobPartner();
			contractJobPartner.setAccount(entity.getJobManager());
			contractJobPartner.setCreateDt(new Date());
			contractJobPartner.setJobId(entity.getId());
			contractJobPartner.setRole(ConstantDic.JOB_PARTNER_TYPE_MC);
			contractJobPartner.setCreator(entity.getCreator());
			contractJobPartnerService.save(contractJobPartner);
			contractJobPartnerDao.flush();
		}
		if (ObjectUtils.isNotEmpty(entity.getJobCooper())){
			String [] jobCooperArr = entity.getJobCooper().split(",");
			for(String jobCooper : jobCooperArr){
				ContractJobPartner contractJobPartner=new ContractJobPartner();
				contractJobPartner.setAccount(jobCooper);
				contractJobPartner.setCreateDt(new Date());
				contractJobPartner.setJobId(entity.getId());
				contractJobPartner.setRole(ConstantDic.JOB_PARTNER_TYPE_PC);
				contractJobPartner.setCreator(entity.getCreator());
				contractJobPartnerService.save(contractJobPartner);
				contractJobPartnerDao.flush();
			}
		}
		if (ObjectUtils.isNotEmpty(entity.getJobAssist())){
			String [] jobAssistArr = entity.getJobAssist().split(",");
			for(String jobAssist : jobAssistArr){
				ContractJobPartner contractJobPartner = new ContractJobPartner();
				contractJobPartner.setAccount(jobAssist);
				contractJobPartner.setCreateDt(new Date());
				contractJobPartner.setJobId(entity.getId());
				contractJobPartner.setRole(ConstantDic.JOB_PARTNER_TYPE_BA);
				contractJobPartner.setCreator(entity.getCreator());
				contractJobPartnerService.save(contractJobPartner);
				contractJobPartnerDao.flush();
			}
			
		}
		
	}
	public void update(ContractJob entity) throws DataAccessException{
		//searchProjectService.jobJoinProject(contractJobSearchProject);
		getEntityDao().update(entity);
		getEntityDao().flush();
		//如果有projectId,则将职位加入项目
		if (entity.getProjectId()!=null&&entity.getProjectId().intValue()!=0){			
			ContractJobSearchProject contractJobSearchProject=new ContractJobSearchProject();
			contractJobSearchProject.setProjectId(entity.getProjectId());
			contractJobSearchProject.setJobId(entity.getId());
			searchProjectService.jobJoinProject(contractJobSearchProject);
		}
		
		/**
		 * 同步业绩分配
		 */
		
		//保存MC(职位负责人)，PC(合作顾问)，BA(业务助理)  
		if (ObjectUtils.isNotEmpty(entity.getJobManagerId())&&entity.getJobManagerId().intValue()!=0){
			ContractJobPartner contractJobPartner=(ContractJobPartner)contractJobPartnerDao.getById(entity.getJobManagerId());		
			contractJobPartner.setAccount(entity.getJobManager());
			contractJobPartner.setModifiedDt(new Date());
			contractJobPartner.setModifier(entity.getModifier());
			if (ObjectUtils.isEmpty(entity.getJobManager())) contractJobPartnerService.removeById(entity.getJobManagerId());
			else contractJobPartnerService.update(contractJobPartner);			
		}else if (ObjectUtils.isNotEmpty(entity.getJobManager())){
			ContractJobPartner contractJobPartner=new ContractJobPartner();
			contractJobPartner.setAccount(entity.getJobManager());
			contractJobPartner.setCreateDt(new Date());
			contractJobPartner.setJobId(entity.getId());
			contractJobPartner.setRole(ConstantDic.JOB_PARTNER_TYPE_MC);
			contractJobPartner.setCreator(entity.getCreator());
			contractJobPartnerService.save(contractJobPartner);
		}
		contractJobPartnerDao.flush();
			
	
		//同步业绩分配
		if (ObjectUtils.isNotEmpty(entity.getJobCooper())){  //职位合作人
			List<String> addList = new ArrayList<String>();
			List<ContractJobPartner> delList = contractJobPartnerService.getJobRoleListByJobId(entity.getId(), ConstantDic.JOB_PARTNER_TYPE_PC);
			String [] jobCooperArr = entity.getJobCooper().split(",");
			for(String cooper : jobCooperArr){
				addList.add(cooper);
			}
			if(delList != null && delList.size()>0){
		
				for(int i =0 ; i<delList.size();i++){
					for(int j=0;j<addList.size();j++){
						if(addList.get(j).equals(delList.get(i).getAccount())){
							delList.remove(i);
							addList.remove(j);
						}
					}
				}
			}
			for(ContractJobPartner jobCooper : delList){
				contractJobPartnerService.removeById(jobCooper.getId());
			}
			for(String jobCooper : addList){
				ContractJobPartner contractJobPartner=new ContractJobPartner();
				contractJobPartner.setAccount(jobCooper);
				contractJobPartner.setCreateDt(new Date());
				contractJobPartner.setRole(ConstantDic.JOB_PARTNER_TYPE_PC);
				contractJobPartner.setJobId(entity.getId());
				contractJobPartner.setCreator(entity.getCreator());
				contractJobPartnerService.save(contractJobPartner);
			}
			
		}
		contractJobPartnerDao.flush();
		
	
		if (ObjectUtils.isNotEmpty(entity.getJobAssist())){  //支持助理
			List<String> addList = new ArrayList<String>();
			List<ContractJobPartner> delList = contractJobPartnerService.getJobRoleListByJobId(entity.getId(), ConstantDic.JOB_PARTNER_TYPE_BA);
			String [] jobAssistArr = entity.getJobAssist().split(",");
			for(String s : jobAssistArr){
				addList.add(s);
			}
			if(delList != null && delList.size()>0){
			
				for(int i =0 ; i<delList.size();i++){
					for(int j=0;j<addList.size();j++){
						if(addList.get(j).equals(delList.get(i).getAccount())){
							delList.remove(i);
							addList.remove(j);
						}
					}
				}
			}
			for(ContractJobPartner jobAssist : delList){
				contractJobPartnerService.removeById(jobAssist.getId());
			}
			for(String jobAssist : addList){
				ContractJobPartner contractJobPartner=new ContractJobPartner();
				contractJobPartner.setAccount(jobAssist);
				contractJobPartner.setCreateDt(new Date());
				contractJobPartner.setRole(ConstantDic.JOB_PARTNER_TYPE_BA);
				contractJobPartner.setJobId(entity.getId());
				contractJobPartner.setCreator(entity.getCreator());
				contractJobPartnerService.save(contractJobPartner);
			}
		} 
		contractJobPartnerDao.flush();
			
		jobOfferPartnerService.addPartnerWithJob(entity.getId());  //同步添加
		jobOfferPartnerService.updatePartnerWithJob(entity.getId()); //同步删除
		
	}
	public ContractJob getById(Integer id){
		ContractJob contractJob = (ContractJob)super.getById(id);
		if (contractJob==null) return contractJob;
		if (contractJob.getTitleId()!=null){
			CompanyDeptTitle title=(CompanyDeptTitle)companyDeptTitleDao.getById(contractJob.getTitleId());
			if (title!=null) contractJob.setTitleName(title.getName());			
		}
		if (contractJob.getCustId()!=null){
			
		    Object o=companyDeptTitleDao.getJdbcDao().findFirstByQuery("select b.abbr_name,a.company_id from customer a,company b where a.company_id=b.id and a.id=? ", contractJob.getCustId());
			if (o!=null) {
				contractJob.setCustName((String)((Object[])o)[0]);
				contractJob.setCompanyId((Integer)((Object[])o)[1]);
			}
			
		}
		//取 MC(职位负责人)，PC(合作顾问)，BA(业务助理)
		Object[] values={id,ConstantDic.JOB_PARTNER_TYPE_MC};
		List<ContractJobPartner> l=null;
		l=contractJobDao.getHibernateTemplate().find("select t from ContractJobPartner t  where t.jobId=? and t.role=? order by t.id ", values);
		if (l!=null&&l.size()>0){
			ContractJobPartner contractJobPartner=(ContractJobPartner)l.get(0);
			contractJob.setJobManagerId(contractJobPartner.getId());
			contractJob.setJobManager(contractJobPartner.getAccount());
		}
		Object[] values2={id,ConstantDic.JOB_PARTNER_TYPE_PC};
		l=contractJobDao.getHibernateTemplate().find("select t from ContractJobPartner t  where t.jobId=? and t.role=? order by t.id ", values2);
		if (l!=null&&l.size()>0){
			StringBuffer cooperBuf = new StringBuffer();
			for (ContractJobPartner cooper : l){
				cooperBuf.append(cooper.getAccount()).append(",");
			}
			ContractJobPartner contractJobPartner=(ContractJobPartner)l.get(0);
			//contractJob.setJobCooperId(contractJobPartner.getId());
			contractJob.setJobCooper(cooperBuf.substring(0, cooperBuf.length()-1));
		}
		Object[] values3={id,ConstantDic.JOB_PARTNER_TYPE_BA};
		l=contractJobDao.getHibernateTemplate().find("select t from ContractJobPartner t  where t.jobId=? and t.role=? order by t.id ", values3);
		if (l!=null&&l.size()>0){
			StringBuffer assistBuf = new StringBuffer();
			for (ContractJobPartner assist : l){
				assistBuf.append(assist.getAccount()).append(",");
			}
			//ContractJobPartner contractJobPartner=(ContractJobPartner)l.get(0);
			//contractJob.setJobAssistId(contractJobPartner.getId());
			contractJob.setJobAssist(assistBuf.substring(0, assistBuf.length()-1));
		}
		Object[] values4={contractJob.getContractId()};
		List custList =contractJobDao.getHibernateTemplate().find("select t from CustomerContract t  where t.id=? ", values4);
		if (custList!=null&&custList.size()>0){
			CustomerContract customerContract=(CustomerContract)custList.get(0);
			contractJob.setContractId(customerContract.getId());
			contractJob.setContractNo(customerContract.getNbr());
			
		}
		//System.out.println(contractJob.getJobManagerId()+"--"+contractJob.getJobCooperId()+"--"+contractJob.getJobAssistId());
		return contractJob;
	}
	
	
	public File genJDDocEn(HttpServletRequest request,OnlineUser onlineUser, ContractJob contractJob) {
		// TODO Auto-generated method stub
		return genJDDoc(request,onlineUser,contractJob,ContractJob.jdTypeEn);
	}
	public File genJDDocCn(HttpServletRequest request,OnlineUser onlineUser, ContractJob contractJob) {
		return genJDDoc(request,onlineUser,contractJob,ContractJob.jdTypeCn);
	}
	
	private File genJDDoc(HttpServletRequest request,OnlineUser onlineUser, ContractJob contractJob,int jdType) {
		// TODO Auto-generated method stub
		Map dataMap=new HashMap();
		if (contractJob==null) return null;
		//ContractJob contractJob
		
		//ContractJob contractJob=(ContractJob)contractJobService.getById(contractJobCvRecom.getJobId());
		Map jobMap=new HashMap();

		//查找客户
		
		if (contractJob!=null)  {
			if (contractJob.getCustId()!=null){
				
			    Object o=companyDeptTitleDao.getJdbcDao().findFirstByQuery("select b.name,b.job_profile jobProfile from customer a,company b where a.company_id=b.id and a.id=? ", contractJob.getCustId());
				if (o!=null) {
					contractJob.setCustName((String)((Object[])o)[0]);
					jobMap.put("custName", StringHelper.clearHTMLTag((String)((Object[])o)[0]));
					jobMap.put("jobProfile", StringHelper.clearHTMLTag((String)((Object[])o)[1],true));
				}
				
				if (contractJob.getTitleId()!=null){
					CompanyDeptTitle companyDeptTitle=(CompanyDeptTitle)companyDeptTitleDao.getById(contractJob.getTitleId());
					if (companyDeptTitle!=null){
						List l=companyDeptTitleDao.getHibernateTemplate().find("from CompanyDept t where t.id=? ", companyDeptTitle.getDeptId());
						if (l!=null&&l.size()>0){
							CompanyDept companyDept=(CompanyDept)l.get(0);
							jobMap.put("deptName", StringHelper.clearHTMLTag(companyDept.getName()));
						}
					}
				}
				
				
				
			}
			jobMap.put("name", StringHelper.clearHTMLTag(contractJob.getName()));
			jobMap.put("direct", StringHelper.clearHTMLTag(contractJob.getDirect()));
			if(jdType==ContractJob.jdTypeEn){ //英文
				jobMap.put("workLocation", SystemDicCache.getCodeNameEn("LOCATION", ObjectUtils.toString(contractJob.getWorkLocation())));
			}else  if(jdType==ContractJob.jdTypeCn){ //中文
				jobMap.put("workLocation", SystemDicCache.getCodeName("LOCATION", ObjectUtils.toString(contractJob.getWorkLocation())));
			}
			jobMap.put("duty", StringHelper.clearHTMLTag(contractJob.getDuty(),true));
			jobMap.put("requirement", StringHelper.clearHTMLTag(contractJob.getRequirement(),true));
			dataMap.put("contractJob", jobMap);
		}
		
		SystemUser systemUser=null;
		if (onlineUser!=null) {
			systemUser=systemUserService.findByUsername(onlineUser.getUsername());
			if (systemUser!=null){
				dataMap.put("systemUser", systemUser);
			}
			
		}
		
		
		//开始生成
		Configuration configuration = new Configuration();
		configuration.setDefaultEncoding("utf-8");
		//configuration.setServletContextForTemplateLoading(request.getSession().getServletContext(), "");
		configuration.setClassForTemplateLoading(this.getClass(), "/template");
		configuration.setClassicCompatible(true);
		//
		
		Template t=null;
		try {			
			
//			StringBuffer str=FileHelper.read(this.getClass().getResource("/template/chrecom.ftl").getPath(),"UTF-8");
//			String templateText= str.toString().replaceAll("<!---", "").replaceAll("--->", "");
//			
//			StringTemplateLoader stringTemplateLoader=new StringTemplateLoader();
//			stringTemplateLoader.putTemplate("template", templateText);
			//test.ftl为要装载的模板
			//configuration.setTemplateLoader(stringTemplateLoader);
			if (jdType==ContractJob.jdTypeEn)
			  t = configuration.getTemplate("joben.ftl");
			else
			  t = configuration.getTemplate("job.ftl");
			
			//configuration.s
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		//输出文档路径及名称
		 SimpleDateFormat dateformat = new SimpleDateFormat("yyyyMMdd");  
		 String fileWebPathDir = Environment.UPLOADFILEDIR+"/"+SystemConstant.JOB+"/"+dateformat.format(new Date())+"/"+contractJob.getId();      
         /**得到文件保存目录的真实路径**/     
         String fileRealPathDir = request.getSession().getServletContext().getRealPath(fileWebPathDir);     
         /**根据真实路径创建目录**/     
         File fileSaveDir = new File(fileRealPathDir);
         //fileSaveDir.i
         if(!fileSaveDir.exists()) {    
            fileSaveDir.mkdirs();     
         }   
		String fileName="JOB_"+contractJob.getId()+".doc";
       // contractJobCvRecom.setUploadDoc(fileWebPathDir+"/"+ fileName);
		File outFile = new File(request.getSession().getServletContext().getRealPath(fileWebPathDir+"/"+fileName));
		
		Writer out = null;
		try {
			
			out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile),"utf-8"));
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 
        try {
        	//System.out.println(t.toString());
        	
			t.process(dataMap, out);
		} catch (TemplateException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			
			e.printStackTrace();
			return null;
		}finally{
			if (out!=null)
				try {
					out.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		
		contractJob.setJobFile(fileWebPathDir+"/"+ fileName);
		
		return outFile;
		
	}
	
	public void saveFile(MultipartHttpServletRequest multipartRequest,
			MultipartFile multipartFile, ContractJob contractJob) {
		// TODO Auto-generated method stub
		SimpleDateFormat dateformat = new SimpleDateFormat("yyyyMMdd");     
		  
		//把公司id也加到目录里
	       String fileWebPathDir = Environment.UPLOADFILEDIR+"/"+SystemConstant.JOB+"/"+dateformat.format(new Date())+"/"+contractJob.getId();      
	       /**得到文件保存目录的真实路径**/     
	       String fileRealPathDir = multipartRequest.getSession().getServletContext().getRealPath(fileWebPathDir);     
	       /**根据真实路径创建目录**/     
	       File fileSaveDir = new File(fileRealPathDir);     
	       if(!fileSaveDir.exists()) {    
	           fileSaveDir.mkdirs();     
	       }   
	       /**页面控件的文件流**/     
	       //MultipartFile multipartFile = multipartRequest.getFile("fileRes");      			       			      			       
	       /**上传文件名称**/     
	       String uploadFileName = multipartFile.getOriginalFilename();//    
	         
	       
	       /**获取文件的后缀**/     
	       String suffix = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf(".")+1);     
	       String preName = multipartFile.getOriginalFilename().substring(0,multipartFile.getOriginalFilename().indexOf("."));
	       // 文件类型   
	         
	       // 获取文件大小，以kB为单位   
	       long size = multipartFile.getSize();   
	       Double fSize =Math.round(size/1024.0*100)/100.0;   
	       String fileSize = Double.toString(fSize)+"KB";
	       if (fSize==0) fSize=new Double(1);
	       /**拼成完整的文件保存路径加文件**/     
	       //重新命名
	       if(!StringHelper.isEmpty(suffix))uploadFileName=RandomStringUtil.getString(8)+"."+suffix;
	       else uploadFileName=RandomStringUtil.getString(8);
	       
	       String fileName = fileRealPathDir + File.separator   + uploadFileName;     
	       
	       File file = new File(fileName);      
	          
	       try {     
	           multipartFile.transferTo(file);  //将文件写入到指定的目录   
	       } catch (IllegalStateException e) {     
	           e.printStackTrace();     
	       } catch (IOException e) {            
	           e.printStackTrace();     
	       }    
	       //multipartFile.g
	       contractJob.setJobFile(fileWebPathDir+"/"+ uploadFileName);
	}
	
	public String getNextPositionNo(ContractJob contractJob) {
		//contractJobPartnerDao.getJdbcDao()
		return contractJobDao.getNextPositionNo(contractJob);
	}
	
	public void updateJobPartner(Integer jobId) {
		// TODO Auto-generated method stub
		StringBuffer sql = new StringBuffer();
		List values = new ArrayList();
		//sql.append(" update contract_job a,( select a.job_id,if(a.role='").append(ConstantDic.JOB_PARTNER_TYPE_MC).append("',group_concat((b.name) separator ','),'') as jobManager,if(a.role='").append(ConstantDic.JOB_PARTNER_TYPE_PC).append("',group_concat((b.name) separator ','),'') as jobCooper,if(a.role='").append(ConstantDic.JOB_PARTNER_TYPE_BA).append("',group_concat((b.name) separator ','),'') as jobAssist from contract_job_partner a,system_user b where a.account=b.account  group by a.job_id) b ");
		sql.append(" update contract_job a,( select a.job_id,GROUP_CONCAT(if(a.role='").append(ConstantDic.JOB_PARTNER_TYPE_MC).append("',b.name,null)) as jobManager,GROUP_CONCAT(if(a.role='").append(ConstantDic.JOB_PARTNER_TYPE_PC).append("',b.name,null)) as jobCooper,GROUP_CONCAT(if(a.role='").append(ConstantDic.JOB_PARTNER_TYPE_BA).append("',b.name,null)) as jobAssist from contract_job_partner a,system_user b where a.account=b.account  group by a.job_id) b ");
		sql.append(" set a.job_manager=b.jobManager,a.job_cooper=b.jobCooper,a.job_assist=b.jobAssist where a.id=? and a.id=b.job_id ");
		values.add(jobId);		
		contractJobDao.getJdbcDao().getJdbcTemplate().update(sql.toString(), values.toArray());
		contractJobDao.flush();
	}

	
	public Page queryJobPage(ContractJobQuery query) {
		// TODO Auto-generated method stub
		Page page=contractJobDao.queryJobPage(query);
		List<ContractJob> ContractJoblist = (List) page.getResult();
		//设置cc项目名
		Map m=null;
		for(int i=0;ContractJoblist!=null&&i<ContractJoblist.size();i++){
			m=(Map)ContractJoblist.get(i);	
			
			
			m.put("industry", SystemDicCache.getCodeName("INDUSTRY", ObjectUtils.toString(m.get("industry"))));
			m.put("workLocation", SystemDicCache.getCodeName("LOCATION", ObjectUtils.toString(m.get("workLocation"))));
			m.put("function", SystemDicCache.getCodeName("JOB", ObjectUtils.toString(m.get("function"))));
			m.put("level", SystemDicCache.getCodeName("JOBLEVEL", ObjectUtils.toString(m.get("level"))));
		}
		return page;
	}

	/**
	 * 查询待审核的职位(对官网发布的职位)
	 */
	
	public Page findAuditJobPage(ContractJobQuery query) {
		
		return contractJobDao.findAuditJobPage(query);
	}

	
	public Integer passedAudit(String id,String auditor) {
		// TODO Auto-generated method stub
		return contractJobDao.passedAudit( id,auditor);
	}

	
	public Integer rejectAudit(String id,String auditor) {
		// TODO Auto-generated method stub
		return contractJobDao.rejectAudit(id, auditor);
	}

	@Override
	public boolean hasOfferBill(Integer jobId) {
		// TODO Auto-generated method stub
		return  contractJobDao.hasOfferBill(jobId);
	}
	
	
}
